$stdout.sync = true

require 'fileutils'
require 'json'
require_relative 'lib/login_command'
require_relative 'lib/navigator'
require_relative 'lib/parallel_request_scraper'
require_relative 'lib/request_finder'

class TrackItScraper

  def initialize(base_url, username, password, options={})
    navigator = Navigator.new base_url
    login_command = LoginCommand.new username, password, navigator
    threads = options[:threads] || 1
    @parallel_request_scraper = ParallelRequestScraper.new navigator, login_command, threads
    @request_finder = RequestFinder.new navigator
    @output_dir = options[:output_dir] || default_output_dir
  end

  def scrape_requests_for_users(users, options={})
    request_ids = @request_finder.get_request_ids_for_users users, options
    scrape_requests request_ids
  end

  def scrape_requests_in_range(from_id, to_id)
    request_ids = (from_id..to_id).to_a
    scrape_requests request_ids
  end

  private

  def scrape_requests(request_ids)
    FileUtils.mkdir_p @output_dir
    request_ids = request_ids - existing_request_ids
    @parallel_request_scraper.scrape(request_ids) { |request| write_request_file request }
  end

  def existing_request_ids
    Dir["#@output_dir/**/*.json"].map { |f| File.basename(f, '.*').to_i }
  end

  def write_request_file(request)
    File.write request_file_path(request), request.to_json
    print '.'
  end

  def request_file_path(request)
    File.join @output_dir, "#{request[:id]}.json"
  end

  def default_output_dir
    File.join 'output', Time.now.strftime('%Y-%m-%d-%H-%M')
  end

end